package com.ruoyi.material.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.material.mapper.ZnMaterialCategoryMapper;
import com.ruoyi.common.core.domain.entity.ZnMaterialCategory;
import com.ruoyi.material.service.IZnMaterialCategoryService;

/**
 * 产品类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-22
 */
@Service
public class ZnMaterialCategoryServiceImpl implements IZnMaterialCategoryService
{
    @Autowired
    private ZnMaterialCategoryMapper znMaterialCategoryMapper;

    /**
     * 查询产品类型
     * 
     * @param id 产品类型主键
     * @return 产品类型
     */
    @Override
    public ZnMaterialCategory selectZnMaterialCategoryById(Long id)
    {
        return znMaterialCategoryMapper.selectZnMaterialCategoryById(id);
    }

    /**
     * 查询产品类型列表
     * 
     * @param znMaterialCategory 产品类型
     * @return 产品类型
     */
    @Override
    public List<ZnMaterialCategory> selectZnMaterialCategoryList(ZnMaterialCategory znMaterialCategory)
    {
        return znMaterialCategoryMapper.selectZnMaterialCategoryList(znMaterialCategory);
    }

    /**
     * 新增产品类型
     * 
     * @param znMaterialCategory 产品类型
     * @return 结果
     */
    @Override
    public int insertZnMaterialCategory(ZnMaterialCategory znMaterialCategory)
    {
        znMaterialCategory.setCreateTime(DateUtils.getNowDate());
        return znMaterialCategoryMapper.insertZnMaterialCategory(znMaterialCategory);
    }

    /**
     * 修改产品类型
     * 
     * @param znMaterialCategory 产品类型
     * @return 结果
     */
    @Override
    public int updateZnMaterialCategory(ZnMaterialCategory znMaterialCategory)
    {
        znMaterialCategory.setUpdateTime(DateUtils.getNowDate());
        return znMaterialCategoryMapper.updateZnMaterialCategory(znMaterialCategory);
    }

    /**
     * 批量删除产品类型
     * 
     * @param ids 需要删除的产品类型主键
     * @return 结果
     */
    @Override
    public int deleteZnMaterialCategoryByIds(Long[] ids)
    {
        return znMaterialCategoryMapper.deleteZnMaterialCategoryByIds(ids);
    }

    /**
     * 删除产品类型信息
     * 
     * @param id 产品类型主键
     * @return 结果
     */
    @Override
    public int deleteZnMaterialCategoryById(Long id)
    {
        return znMaterialCategoryMapper.deleteZnMaterialCategoryById(id);
    }

    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<ZnMaterialCategory> znMaterialCategoriesnMaterialCategorys) {
        List<ZnMaterialCategory> returnList = new ArrayList<ZnMaterialCategory>();
        List<Long> tempList = new ArrayList<Long>();
        for (ZnMaterialCategory znMaterialCategory : znMaterialCategoriesnMaterialCategorys)
        {
            tempList.add(znMaterialCategory.getId());
        }
        for (Iterator<ZnMaterialCategory> iterator = znMaterialCategoriesnMaterialCategorys.iterator(); iterator.hasNext();)
        {
            ZnMaterialCategory znMaterialCategory = (ZnMaterialCategory) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(znMaterialCategory.getParentId()))
            {
                recursionFn(znMaterialCategoriesnMaterialCategorys, znMaterialCategory);
                returnList.add(znMaterialCategory);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = znMaterialCategoriesnMaterialCategorys;
        }
        return returnList.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<Long> getListByParentId(Long parentId) {
        List<Long> idList = new ArrayList<Long>();
        List<ZnMaterialCategory> list = znMaterialCategoryMapper.getListByParentId(parentId);
        idList.add(parentId);
        if(list!=null && list.size()>0) {
            getIdListByParentId(idList, parentId);
        }
        return idList;
    }
    public List<Long> getIdListByParentId(List<Long> idList, Long parentId){
        List<ZnMaterialCategory> list = znMaterialCategoryMapper.getListByParentId(parentId);
        if(list!=null && list.size()>0) {
            for(ZnMaterialCategory mc : list){
                idList.add(mc.getId());
                getIdListByParentId(idList, mc.getId());
            }
        }
        return idList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<ZnMaterialCategory> list, ZnMaterialCategory t)
    {
        // 得到子节点列表
        List<ZnMaterialCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (ZnMaterialCategory tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 得到子节点列表
     */
    private List<ZnMaterialCategory> getChildList(List<ZnMaterialCategory> list, ZnMaterialCategory t)
    {
        List<ZnMaterialCategory> tlist = new ArrayList<ZnMaterialCategory>();
        Iterator<ZnMaterialCategory> it = list.iterator();
        while (it.hasNext())
        {
            ZnMaterialCategory n = (ZnMaterialCategory) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<ZnMaterialCategory> list, ZnMaterialCategory t)
    {
        return getChildList(list, t).size() > 0;
    }
}
